package com.labprog.oncapintada.persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.labprog.oncapintada.exceptions.ClasseNaoEncontradaException;
import com.labprog.oncapintada.exceptions.ClasseNaoInseridaException;
import com.labprog.oncapintada.exceptions.ErroGeralException;
import com.labprog.oncapintada.models.Classe;

public class PersistenciaClasse extends AbstractPersistence {

	private ClaClasseDAO dao;

	public PersistenciaClasse() {
		dao = new ClaClasseDAO();
	}

	public void inserirClasse(Classe classe) throws ClasseNaoInseridaException {
		// Integer count = 1;
		// Connection conn = null;
		// StringBuffer SQL = new StringBuffer();
		// PreparedStatement stmt = null;
		//		
		//		
		//		
		// SQL.append("INSERT INTO mercadex.cla_classe (cla_desc,cla_nome)
		// VALUES (?, ?)");
		//		
		// try {
		// conn = super.getConnection();
		// stmt = conn.prepareStatement(SQL.toString());
		//			
		// stmt.setString(count++, classe.getNome());
		// stmt.setString(count++, classe.getDescricao());
		//			
		// stmt.executeUpdate();
		// } catch (SQLException e) {
		// throw new ClasseNaoInseridaException(classe, e);
		// }

		Session sessao = HibernateSessionFactory.getSession();
		Transaction transaction = sessao.beginTransaction();

		dao.save(classe.getClasseHibernate());

		transaction.commit();

		sessao.close();
	}

	public Classe obterdetalhesClasse(Classe classe)
			throws ClasseNaoEncontradaException, ErroGeralException {
		// Integer count = 1;
		// Connection conn = null;
		// StringBuffer SQL = new StringBuffer();
		// PreparedStatement stmt = null;
		// ResultSet resultSet;
		//		
		// SQL.append("SELECT cla.cla_id as id, \n");
		// SQL.append(" cla.cla_nome as nome, \n");
		// SQL.append(" cla.cla_desc as descricao \n");
		// SQL.append("FROM mercadex.cla_classe cla \n");
		// SQL.append("WHERE cla.cla_id = ?");
		//		
		// try {
		// conn = super.getConnection();
		// stmt = conn.prepareStatement(SQL.toString());
		//			
		// stmt.setInt(count++, classe.getId());
		//			
		// resultSet = stmt.executeQuery();
		//			
		// if (resultSet.next()) {
		//							
		// classe.setId(resultSet.getInt("id"));
		// classe.setDescricao(resultSet.getString("descricao"));
		// classe.setNome(resultSet.getString("nome"));
		//				
		// } else {
		// throw new ClasseNaoEncontradaException(classe);
		// }
		//			
		//			
		// } catch (SQLException e) {
		// throw new ErroGeralException();
		// }
		// return classe;
		
		ClaClasse classeHibernate = dao.findById(classe.getId()); 
		
		classe.setClasseHibernate(classeHibernate);
		
		return classe;
	}

	public List obterClasses() {
		List resultado = new ArrayList();
//		Integer count = 1;
//		Connection conn = null;
//		StringBuffer SQL = new StringBuffer();
//		PreparedStatement stmt = null;
//		ResultSet resultSet;
//
//		SQL.append("SELECT  * \n");
//		SQL.append("FROM    mercadex.cla_classe cla \n");
//
//		try {
//			conn = super.getConnection();
//			stmt = conn.prepareStatement(SQL.toString());
//
//			resultSet = stmt.executeQuery();
//
//			while (resultSet.next()) {
//				Classe classe = new Classe();
//				classe.setId(resultSet.getInt("cla_id"));
//				classe.setDescricao(resultSet.getString("cla_desc"));
//				classe.setNome(resultSet.getString("cla_nome"));
//				resultado.add(classe);
//			}
//
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
		
		List resultadoHibernate = dao.findAll();
		Classe aux;
		for (Iterator iter = resultadoHibernate.iterator(); iter.hasNext();) {
			ClaClasse classeHibernate = (ClaClasse) iter.next();
			
			aux = new Classe();
			aux.setClasseHibernate(classeHibernate);
			resultado.add(aux);
			
		}
		
		return resultado;
	}

}
